package com.love_weinuo.util.jpushutil;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.lzy.okgo.callback.StringCallback;
import com.love_weinuo.util.HTTPAPI;
import com.love_weinuo.util.LogUtil;
import com.love_weinuo.util.SpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator
 * on 2019/2/22.
 */
public class MyReceiver extends BroadcastReceiver {

    Context mContext;

    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        Bundle bundle = intent.getExtras();
        LogUtil.e("极1 message:" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
        LogUtil.e("光1 extra  :" + bundle.getString(JPushInterface.EXTRA_EXTRA));

        if (!TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA)) && bundle.getString(JPushInterface.EXTRA_EXTRA) != null) {
            if (bundle.getString(JPushInterface.EXTRA_EXTRA).contains("lizard")) {
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
                    String result = jsonObject.getString("lizard");
                    LogUtil.e("非汉字解析： " + result);
                } catch (JSONException e) {
                    e.printStackTrace();
                    LogUtil.e("解析错误");
                }
            } else {
                LogUtil.e("推送消息不包含lizard");
            }
        }

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            LogUtil.e("极2 message:" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            LogUtil.e("光2 extra  :" + bundle.getString(JPushInterface.EXTRA_EXTRA));
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            LogUtil.e("[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            LogUtil.e("极3 message:" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            LogUtil.e("光3 extra  :" + bundle.getString(JPushInterface.EXTRA_EXTRA));
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            LogUtil.e("极4 message:" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            LogUtil.e("光4 extra  :" + bundle.getString(JPushInterface.EXTRA_ALERT));
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            LogUtil.e("极5 message:" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            LogUtil.e("光5 extra  :" + bundle.getString(JPushInterface.EXTRA_ALERT));
            LogUtil.e("用户点开了通知" + bundle.getString(JPushInterface.EXTRA_EXTRA));

            //等候传递过来的拼单ID,邀请码Code
            //            getPindanDesc("","");


            //            if (bundle.getString(JPushInterface.EXTRA_EXTRA).contains("lizard")) {
            //                JSONObject jsonObject = null;
            //                try {
            //                    jsonObject = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
            //                    String result = jsonObject.getString("lizard");
            //                    LogUtil.e("非汉字解析： " + result);
            //                } catch (JSONException e) {
            //                    e.printStackTrace();
            //                    LogUtil.e("用户点开通知操作错误" + e.getMessage());
            //                }
            //            } else {
            //                LogUtil.e("推送消息不包含lizard");
            //            }
        }
            //        else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
            //            //                LogUtil.e("极6 message:" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            //            //                LogUtil.e("光6 extra  :" + bundle.getString(JPushInterface.EXTRA_ALERT));
            //            //                LogUtil.e("[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_ALERT));
            //        } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
            //            //                LogUtil.e("极7 message:" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            //            //                LogUtil.e("光7 extra  :" + bundle.getString(JPushInterface.EXTRA_ALERT));
            //            //                boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
            //            //                LogUtil.e("[MyReceiver]" + intent.getAction() + " connected state change to " + connected);
            //        } else {
            //            //                LogUtil.e("极8 message:" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            //            //                LogUtil.e("光8 extra  :" + bundle.getString(JPushInterface.EXTRA_ALERT));
            //            //                LogUtil.e("[MyReceiver] Unhandled intent - " + intent.getAction());
            //        }
    }

    String userId = (String) SpUtils.get("module_user_id", "");

    public void getPindanDesc(String pindanId, final String inviteCode) {
        HTTPAPI.getInstance().getPidanDesc(userId, pindanId, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtil.e("拼单详情获取成功" + s);
                sentBroadcastDelete(s + "&" + inviteCode);
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                LogUtil.e("拼单详情获取失败" + e.getMessage());
            }
        });
    }

    Intent intentDelete;

    public void sentBroadcastDelete(String s) {
        if (intentDelete == null) {
            intentDelete = new Intent();
            intentDelete.setAction("moduleScan");
        }
        intentDelete.putExtra("pindanId", s);
        mContext.sendBroadcast(intentDelete);
    }

}



